package com.cyx.fruit.dao.impl;


import com.cyx.fruit.dao.FruitDAO;
import com.cyx.fruit.mySSM.basedao.basedao;
import com.cyx.fruit.pojo.Fruit;

import java.sql.*;
import java.util.List;

public class FruitDAOImpl extends basedao<Fruit> implements FruitDAO {

    @Override
    public List<Fruit> getFruitList(String keyword,Integer pageNumber) {
        //每页显示5条记录，对于水果名称、水果介绍进行模糊查询
        String sql = "select * from t_fruit where fname like ? or remark like ? limit ?,5";

        return super.executeQuery(sql,"%"+keyword+"%" ,"%"+keyword+"%",(pageNumber - 1) * 5 );
    }

    @Override
    public boolean addFruit(Fruit fruit) {
        String sql = "insert into t_fruit values(0,?,?,?,?)";
        return super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark()) > 0;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {
        String sql = "update t_fruit set fname = ?,price = ?,fcount = ?,remark = ? where fid = ? ";
        return super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid()) > 0;
    }

    @Override
    public Fruit getFruitByFid(Integer fid) {
        String sql = "select * from t_fruit where fid = ? ";
        return super.load(sql, fid);
    }


    @Override
    public boolean delFruit(Integer fid) {
        String sql = "delete from t_fruit where fid =? ";
        return super.executeUpdate(sql, fid) > 0;
    }

    /**
     * 获取水果库存的记录数
     * @return
     */
    @Override
    public int getFruitCount(String keyword) {
        String sql = "select count(*) from t_fruit where fname like ? or remark like ?";
        Object[] value = super.executeComplexQuery(sql,"%"+keyword+"%","%"+keyword+"%");
        int fruitCount = ((Long) value[0]).intValue();
        return fruitCount;
    }
}
